package com.jiaeda.shiromaster.realm;

import com.jiaeda.shiromaster.entity.SysPermission;
import com.jiaeda.shiromaster.entity.SysRole;
import com.jiaeda.shiromaster.entity.SysUser;
import com.jiaeda.shiromaster.service.SysPermissionService;
import com.jiaeda.shiromaster.service.SysRoleService;
import com.jiaeda.shiromaster.service.SysUserService;
import lombok.Getter;
import lombok.Setter;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by chengchaopeng on 2018/5/26.
 */

@Repository
@Getter
@Setter
public class CustomRealm extends AuthorizingRealm {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private SysPermissionService sysPermissionService;

//    Map<String, String> userMap = new HashMap<>(16);

    {
//        userMap.put("Geoccp", "8ba7d14c8f97c5c69e391ed44dbd37ac");
//        super.setName("custonRealm");
    }
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        //1、从主体传递过来的认证信息获取用户名
        String loginAccount = (String) principalCollection.getPrimaryPrincipal();
        //从数据库或者缓存中获取角色数据
        Set<String> roles = getRolesByLoginAccount(loginAccount);

        Set<String> permissions = getPermissionsByLoginAccount(loginAccount);
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        //设置角色信息
        authorizationInfo.setRoles(roles);
        //设置权限信息
        authorizationInfo.setStringPermissions(permissions);
        return authorizationInfo;
    }

    /**
     * 根据用户名获取权限数据
     *
     * @param loginAccount
     * @return
     */
    private Set<String> getPermissionsByLoginAccount(String loginAccount) {
        List<SysPermission> sysPermissionList=sysPermissionService.findByLoginAccount(loginAccount);
        Set<String> sets = new HashSet<>();
        sysPermissionList.forEach(sysPermission -> {
            sets.add(sysPermission.getMenuCode());
        });
        return sets;
    }

    /**
     * 根据用户名获取角色名
     *
     * @param loginAccount
     * @return
     */
    private Set<String> getRolesByLoginAccount(String loginAccount) {
        List<SysRole> sysRoleList=sysRoleService.findByLoginAccount(loginAccount);
        Set<String> sets = new HashSet<>();
        sysRoleList.forEach(sysRole -> {
            sets.add( sysRole.getRoleKey());
        });
        return sets;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        //1、从主体传递过来的认证信息获取用户名
        String loginAccount = (String) authenticationToken.getPrincipal();
        //2、通过用户名获得密码
        String password = getPasswordByLoginAccount(loginAccount);
        if (password == null)
            return null;
        SimpleAuthenticationInfo authenticationInfo = new
                SimpleAuthenticationInfo(loginAccount, password, "custonRealm");
        //设置加盐常量
        authenticationInfo.setCredentialsSalt(ByteSource.Util.bytes("geoccp"));
        return authenticationInfo;
    }

    /**
     * 获取用户密码
     *
     * @param loginAccount
     * @return
     */
    private String getPasswordByLoginAccount(String loginAccount) {
        SysUser sysUser=sysUserService.findByLoginAccount(loginAccount);
        return sysUser.getLoginPass();
    }



//    public static void main(String[] args) {
//        //加密 加盐
//        Md5Hash md5Hash = new Md5Hash("123456", "geoccp");
//        System.out.println(md5Hash.toString());
//    }
}
